﻿using System.IO;
using GameFramework.common;
using UnityEditor;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;

namespace GameFramework.configs
{
    public class LocalConfig:Singleton<LocalConfig>
    {
        public MsgConfig msgConfig;
        public UpdateConfig updateConfig;
        public void loadConfig(string localConfigPath)
        {
            var deserializer = new DeserializerBuilder()
                .WithNamingConvention(CamelCaseNamingConvention.Instance)
                .Build();
            AsyncOperationHandle<TextAsset> async = Addressables.LoadAssetAsync<TextAsset>(localConfigPath);
            async.WaitForCompletion();
            if (async.Status != AsyncOperationStatus.Succeeded)
            {
                Debug.LogError("LocalConfig加载失败："+async.OperationException.Message);
                return;
            }
            var textAsset = async.Result;
            LocalConfig config = deserializer.Deserialize<LocalConfig>(textAsset.text);
            //TODO 这样内存中就有2份LocalConfig对象了，需要优化下
            msgConfig = config.msgConfig;
            Debug.Log("配置文件LocalConfig加载完成！");
        }


    }
}